<?php
/**
 * Created by JetBrains PhpStorm.
 * User: scala
 * Date: 12-10-23
 * Time: 下午4:31
 * To change this template use File | Settings | File Templates.
 */
class MessageModel extends Model
{
    protected $_validate=array(
        array("customname","require","您的昵称不能为空！"),
        array("customname","checkTrimNotEmpty","你的昵称不能为空！",Model::MUST_VALIDATE,"callback",Model::MODEL_INSERT),
        array("customname","1,50","您的昵称请在1-50位！",Model::MUST_VALIDATE,"length",Model::MODEL_INSERT),
        array("content","require","您的留言不能为空!"),
        array("content","checkTrimNotEmpty","您的留言不能为空！",Model::MUST_VALIDATE,"callback",Model::MODEL_INSERT),
        array("createip","checkTodayMessageCount","此ip超出了每天留言次数，每天最多留言15次!",Model::MUST_VALIDATE,"callback"),
        array("verify","require","验证码必须输入!"),
        array("verify","checkVerify","验证码输入错误，请重新输入!",Model::MUST_VALIDATE,"callback"),
    );

    function checkVerify($data){
        if(trim($data)==""){
            return false;
        }
        if($_SESSION["verify"]!=md5($_POST["verify"])){
            return false;
        }else{
            return true;
        }
    }

    function checkTrimNotEmpty($data){
        if(trim($data)==""){
            return false;
        }else{
            return true;
        }
    }


    function checkTodayMessageCount($data){
        $clientIP=get_client_ip();
        $message=new MessageModel();
        $map["createip"]=array("eq",$clientIP);
        $year=date("Y",time());
        $month=date("m",time());
        $day=date("d",time());
        $startTimeStr=$year."-".$month."-".$day." 00:00:00";
        $endTimeStr=$year."-".$month."-".$day." 23:59:59";
        $map["createtime"]=array("BETWEEN",array(strtotime($startTimeStr),strtotime($endTimeStr)));
        $count=$message->where($map)->count();
        if($count>=15){
            return false;
        }else{
            return true;
        }
    }


    protected $_auto=array(
        array("createtime","time",Model::MODEL_INSERT,"function"),
        array("createip","get_client_ip",Model::MODEL_INSERT,"function"),
    );
}
